$(function () {
	getpostList();
});
var exchangeIds;
/*重置*/
function resetClick(){
    $("#title").val("");
    $("#userState option:selected").val("全部");
    $('#table2').addClass("disnone");
    getpostList();
}
/*查询*/
function getpostList(init){
    JY.Model.loading();
	$("#ticktTable tbody").html("");
    var pageNum=$("#baseForm .pageNum").val();
    var pageSize=$("#baseForm .pageSize").val();
	var title =$("#title").val().trim();
	var userState=$("#userState option:selected").val();
	if(userState=="全部"){
        userState="";
    }
	$.ajax({
		url : jypath + "/communication/quertCommunicationListMrg",
		type : "post",
		data:{
            "pageNum":pageNum,
            "pageSize":pageSize,
            "title":title,
            "state":userState,
        },
		jsonType : "json",
		success : function(data) {
		    var data=data instanceof Object ? data : JSON.parse(data);
		    var html="";
            var list=data.obj;
            var results=list.results;
            var pageNum=list.pageNum,
                pageSize=list.pageSize,
                totalRecord=list.totalRecord;
            console.log(results);
            if(results!=null&&results.length>0){
            	 var leng=(pageNum-1)*pageSize;//计算序号
            	 for(var i =0;i<results.length;i++){
	            	var lit = results[i];
	            	html+="<tr class='tr_color'>";
	            	 html+="<td class='center ver-m'><label><input type='checkbox' name='ids' value='"+lit.exchangeId+"' class='ace' /> <span class='lbl'></span></label></td>";
	            	 html+="<td class='center'>"+lit.userName+"</td>";
	            	 html+="<td class='center'>"+lit.createTimeString+"</td>";
	            	 html+="<td class='center'>"+lit.title+"</td>";
	            	 html+="<td class='center reps'>"+lit.answersCount +"</td>";
	            	if(lit.typeOrder==0){
	                     html+="<td class='center'>官方</td>";
	                 }else if(lit.typeOrder==1){
	                     html+="<td class='center'>热门</td>";
	                 }else if(lit.typeOrder==2){
	                     html+="<td class='center'>推荐</td>";
	                 }else if(lit.typeOrder==3){
	                     html+="<td class='center'>普通</td>";
	                 }
	            	 if(lit.state==1){
	                     html+="<td class='center'>有效</td>";
	                 }else if(lit.state==0){
	                     html+="<td class='center'>无效</td>";
	                 }
	            	 html+="<td class='center details'>发表内容详情</td>";
	            	 html+="</tr>";
	            }
	            $("#ticktTable tbody").append(html);	
	            JY.Page.setPage("baseForm","pageing",pageSize,pageNum,totalRecord,"getpostList");
	        }else{
            	 html+="<tr><td colspan='7' class='center'>没有相关数据</td></tr>";
                 $("#ticktTable tbody").append(html);
                 $("#baseForm ul").empty();//清空分页
            }
            JY.Model.loadingClose();
		}
	})

}

// 帖子启用禁用
function enAbleClick(type){
    var chks =[];
    $('#ticktTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
     });
     if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length>0){
        var states="";
        if(type==1){
            //启用
            states=1;
        }else if(type==0){
            //禁用
            states=0;
        }
        JY.Model.message("auForm_edit","修改状态",function(){
            var that = $(this);
            $.ajax({
                url : jypath + "/communication/deleteExchangeBychangeId",
                type : "post",
                data:{
                    "state":states,
                    "exchangeId":chks
                },
                jsonType : "json",
                success : function(data) {
                   console.log(data);
                   if(data.res==1){
                       that.dialog("close");
                       JY.Model.info("操作成功!",function(){getpostList();});
                   }
                }
            })
        });
    }
}
//评论启用禁用
function AbleClick(type){
    var chks =[];
    $('#replyTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
     });
     if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length>0){
    	console.log(chks);
        var states="";
        if(type==1){
            //启用
            states=1;
        }else if(type==0){
            //禁用
            states=0;
        }
        JY.Model.message("auForm_edit","修改状态",function(){
        	 var that = $(this);
            $.ajax({
                url : jypath + "/communication/deleteAnswerByAnswerId",
                type : "post",
                data:{
                    "state":states,
                    "answerId":chks
                },
                jsonType : "json",
                success : function(data) {
                   console.log(data);
                   if(data.res==1){
                       that.dialog("close");
                       JY.Model.info("操作成功!",function(){replynu(exchangeIds);});
                   }
                }
            })
        });
    }
}

function replynu(exchangeId){
	$("#replyTable tbody").html("");
    var pageNum=$("#baseForms .pageNum").val();
    var pageSize=$("#baseForms .pageSize").val();
	$.ajax({
        url : jypath + "/communication/quertCommunicationDetailMrg",
        type : "post",
        data:{
        	 "pageNum":pageNum,
             "pageSize":pageSize,
             "exchangeId":exchangeId
        },
        jsonType : "json",
        success : function(data){
        	if(data.res == 1){}
        	$('#table2').removeClass('disnone');
        	var data=data instanceof Object ? data : JSON.parse(data);
        	console.log(data);
        	var html="";
            var list=data.obj;
            var results=list.results;
            var pageNum=data.obj.pageNum,
                pageSize=data.obj.pageSize,
                totalRecord=data.obj.totalRecord;
            console.log(results.length);
            if(results.length > 0){
	            for(var i =0;i<results.length;i++){
	            	var lit = results[i];
	            	html+="<tr>";
	            	 html+="<td class='center ver-m'><label><input type='checkbox' name='ids' value='"+lit.answerId+"' class='ace' /> <span class='lbl'></span></label></td>";
	            	 html+="<td class='center'>"+lit.parentName+"</td>";
	            	 html+="<td class='center'>"+lit.userName+"</td>";
	            	 html+="<td class='center'>"+lit.answerDes+"</td>";
	            	 html+="<td class='center'>"+lit.createTimeString+"</td>";
	            	 if(lit.state==1){
	                     html+="<td class='center'>有效</td>";
	                 }else if(lit.state==0){
	                     html+="<td class='center'>无效</td>";
	                 }
	            	 html+="</tr>";
	            	 
	            }
	            $("#replyTable tbody").append(html);	
	           	JY.Page.setPage("baseForms","pageings",pageSize,pageNum,totalRecord,"replynu("+exchangeId+")");
	        }else{
            	 html+="<tr><td colspan='8' class='center'>没有相关数据</td></tr>";
                 $("#replyTable tbody").append(html);
                 $("#baseForms ul").empty();//清空分页
            }
            
        }
    })
}


$('body').on('click','.reps',function(){
	var zhi = $(this).text();
	$(this).parent().addClass("hascolor").siblings(".tr_color").removeClass("hascolor");
	if(zhi <= 0){
		JY.Model.info("该条帖子暂时还没有回复!");
		$("#replyTable tbody").html("");	
		$('#table2').addClass('disnone');
	}else{
		exchangeIds = $(this).siblings().find('input').val();
		replynu(exchangeIds);
	}
})

function clearForm(){
    $("#addVaccineForm input[name='vaccine_name']").val("");
    $("#addVaccineForm .textVaccine").val("");
}

//查看详情内容弹窗
$('body').on('click','.details',function(){
	$('#details ul').html("");
    var exchangeId = $(this).siblings().find('.ace').val();
    $.ajax({
        url : jypath + "/communication/quertExchangenDetailMrg",
        type : "post",
        data:{
            "exchangeId":exchangeId,
            "userId":1
        },
        jsonType : "json",
        success : function(data) {
        	var data=data instanceof Object ? data : JSON.parse(data);
        	var li = data.obj;
            var html ="";
            if(data.res==1){
            	// for(var j=0;j<li.length;j++){
            	    console.log(data);
            		if(li.ymAttachments.length > 0){
                		for(var i = 0;i<li.ymAttachments.length;i++){
                			var ll = li.ymAttachments[i];
                			html += '<li style="float: left;width:30%"><img onclick="enlarge(this)" class="imgUrls" src="'+ll.attachmentUrl+'" style="width:100px;height:100px"></li>';
                		}
                		$('#details ul').append(html);
                   }
                    console.log(data.obj.ecchangeMessage);
            		if(data.obj.ecchangeMessage != null || data.obj.ecchangeMessage != undefined){
                    $('#details p').html(data.obj.ecchangeMessage)
                     }else{
                        $('#details p').html("无内容!")
                    }

            	}
            // }
            JY.Model.check("details","查看详情",function(){})
        }
    })
   
})
//放大图片
function enlarge(obj){
    var imgUrl=$(obj).attr("src");
    $(".bigImg").attr("src",imgUrl);
    $(".imgBack").removeClass("hide");
}
function closeImg(){
    $(".imgBack").addClass("hide");
}
function enAble(){
	var chks =[];
	var typeOrder= [];
    $('#ticktTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
     });
     if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length>0){
    	JY.Model.message("thelabel","修改帖子标签",function(){
    		var type = $("#post").find("option:selected").val();
        	for(var j=0;j<chks.length;j++){
        		typeOrder.push(type);
        	}
        	console.log(typeOrder);
          	 var that = $(this);
              $.ajax({
                  url : jypath + "/communication/updateTypeOrder",
                  type : "post",
                  data:{
                      "typeOrder":typeOrder,
                      "exchangeId":chks
                  },
                  jsonType : "json",
                  success : function(data) {
                     console.log(data);
                     if(data.res==1){
                         that.dialog("close");
                         JY.Model.info("操作成功!",function(){getpostList()});
                     }
                  }
              })
          });
    }
}













